// import { stringify } from 'querystring';
// import { history } from 'umi';
// import { fakeAccountLogin } from '@/services/login';
// import { setAuthority } from '@/utils/authority';
// import { getPageQuery } from '@/utils/utils';
// import { message } from 'antd';
// const Model = {
//   namespace: 'login',
//   state: {
//     status: undefined,
//   },
//   effects: {
//     *login({ payload }, { call, put }) {
//       const response = yield call(fakeAccountLogin, payload);
//       yield put({
//         type: 'changeLoginStatus',
//         payload: response,
//       }); // Login successfully
//
//       if (response.status === 'ok') {
//         const urlParams = new URL(window.location.href);
//         const params = getPageQuery();
//         message.success('🎉 🎉 🎉  登录成功！');
//         let { redirect } = params;
//
//         if (redirect) {
//           const redirectUrlParams = new URL(redirect);
//
//           if (redirectUrlParams.origin === urlParams.origin) {
//             redirect = redirect.substr(urlParams.origin.length);
//
//             if (window.routerBase !== '/') {
//               redirect = redirect.replace(window.routerBase, '/');
//             }
//
//             if (redirect.match(/^\/.*#/)) {
//               redirect = redirect.substr(redirect.indexOf('#') + 1);
//             }
//           } else {
//             window.location.href = '/';
//             return;
//           }
//         }
//
//         history.replace(redirect || '/');
//       }
//     },
//
//     logout() {
//       const { redirect } = getPageQuery(); // Note: There may be security issues, please note
//
//       if (window.location.pathname !== '/user/login' && !redirect) {
//         history.replace({
//           pathname: '/user/login',
//           search: stringify({
//             redirect: window.location.href,
//           }),
//         });
//       }
//     },
//   },
//   reducers: {
//     changeLoginStatus(state, { payload }) {
//       setAuthority(payload.currentAuthority);
//       return { ...state, status: payload.status, type: payload.type };
//     },
//   },
// };
// export default Model;

// add20210623@author:lf重写登录model
import {login as loginService, register as registerService} from '@/services/login'
import {history} from 'umi'
import {message} from 'antd'

const namespace = 'login';
const selectState = state => state[namespace];

const Model = {
  namespace,
  state: {
    username: '',
    password: '',
  },
  effects: {
    * login(_, {call, select}) {

      const state = yield select(selectState);
      const result = yield call(loginService, state);
      if (result.code === 200) {
        localStorage.setItem('token', result.data.token);
        history.push('/')
      } else {
        message.error(result.msg)
      }
    },
    * register(_, {call, select}) {
      const state = yield select(selectState);
      const result = yield call(registerService, state);
      if (result.code === 200) {
        message.success('注册成功');
      } else {
        message.error(result.msg)
      }
    }
  },
  reducers: {
    overrideStateProps(state, {payload}){
      return {
        ...state,
        ...payload,
      }
    }
  }
};

export default Model;
